package sg.edu.nus.iss.billing.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Thurein
 * @author Zifeng
 */
public class DateHelper {

	public static Date addDates(Date date, int days) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		can.add(Calendar.DATE, days);
		return can.getTime();
	}

	public static Date getDate(String ddMMYYYY) {
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		Date date = new Date();

		try {
			date = df.parse(ddMMYYYY);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date getDate(String dateStr, String format) {
		DateFormat df = new SimpleDateFormat(format);
		Date date = new Date();

		try {
			date = df.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static long getDaysInBetween(Date date1, Date date2) {

		// Calculate difference in days
		long diffDays = (date1.getTime() - date2.getTime())/ (24 * 60 * 60 * 1000);
		return (Math.abs(diffDays) + 1);
	}

	public static Date lastDateofMonth(int year, int month, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		calendar.set(year, month, maxDay);
		return calendar.getTime();
	}
}